package org.hgvc.webapp.ets.spring.dao;

import java.util.Date;
import java.util.List;

import org.hgvc.webapp.ets.bo.Package;
import org.hgvc.webapp.ets.bo.PackagePeakRates;
import org.hgvc.webapp.ets.bo.PackageProducts;
import org.hgvc.webapp.ets.constant.AdminConstant;
import org.hgvc.webapp.ets.constant.CommonConstant;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.jocsb.base.AbstractHibernateDao;
import org.jocsb.base.util.DateUtil;


public class PackageHbmDao extends AbstractHibernateDao{
	public List<Package> getPackageList()throws Exception{
		Criteria criteria = getSession().createCriteria(Package.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		List<Package> list=criteria.list();
		return list;
	}
	
	public List<PackageProducts> getPackageProductsList(Package packageP)throws Exception{
		Criteria criteria = getSession().createCriteria(PackageProducts.class);
		criteria.add(Restrictions.eq("packages.packageId", packageP.getPackageId()));
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		List<PackageProducts> list=criteria.list();
		return list;
	}
	
	public List<PackagePeakRates> getPackagePeakRatesList(Package packageP)throws Exception{
		Criteria criteria = getSession().createCriteria(PackagePeakRates.class);
		criteria.add(Restrictions.eq("packages.packageId", packageP.getPackageId()));
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		List<PackagePeakRates> list=criteria.list();
		return list;
	}
	
	public List<PackagePeakRates> getPackagePeakRatesList(Package packageP,String peakType)throws Exception{
		Criteria criteria = getSession().createCriteria(PackagePeakRates.class);
		criteria.add(Restrictions.eq("packages.packageId", packageP.getPackageId()));
		criteria.add(Restrictions.eq("peakType", peakType));
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		List<PackagePeakRates> list=criteria.list();
		return list;
	}
	
	public List<Package> getPackageList(String searchField,String searchKeyword)throws Exception{
		if (searchKeyword==null) searchKeyword="";
		if (searchField==null) searchField="";
		Criteria criteria = getSession().createCriteria(Package.class);
		if (searchField.equalsIgnoreCase(AdminConstant.SEARCH_CODE))
			criteria.add(Restrictions.ilike("packageCode", "%"+searchKeyword+"%"));
		if (searchField.equalsIgnoreCase(AdminConstant.SEARCH_DESC))
			criteria.add(Restrictions.ilike("packageDesc", "%"+searchKeyword+"%"));
		List<Package> list=criteria.list();
		return list;
	}
	
	public PackagePeakRates getPackagePeakRates(Package packagesP,String peakType,Date date)throws Exception{
		Criteria criteria = getSession().createCriteria(PackagePeakRates.class);
		criteria.add(Restrictions.eq("isDeleted", CommonConstant.NOT_DELETED));
		criteria.add(Restrictions.eq("packages.packageId", packagesP.getPackageId()));
		criteria.add(Restrictions.eq("peakType", peakType));
		Date newDate = DateUtil.formatDateByTime(date, 0, 0, 0, 0);
		criteria.add(Restrictions.le("effectiveDate", newDate));
		criteria.add(Restrictions.ge("expiryDate", newDate));
		
		PackagePeakRates packagePeakRates = null;
		List<PackagePeakRates> list = criteria.list();
		
		if (list.size()>0)
			packagePeakRates=list.get(0);
		return packagePeakRates;
	}
}
